import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards, NotFoundException, BadRequestException } from '@nestjs/common';
import { VirtualCharacterService } from './virtual-character.service';
import { CreateVirtualCharacterDto, UpdateVirtualCharacterDto } from './dto/virtual-character.dto';
import { CurrentUser } from '../common/decorators/user.decorator';
import { ERROR_CODE, ERROR_MESSAGE, getResponse, SUCCESS_CODE, SUCCESS_MESSAGE } from 'src/common/const/const';
import { AuthGuard } from '../common/guards/auth.guard';

@Controller('virtual-character')
@UseGuards(AuthGuard)
export class VirtualCharacterController {
  constructor(private readonly virtualCharacterService: VirtualCharacterService) {}

  @Post('create')
  async create(
    @CurrentUser() userId: string,
    @Body() createDto: CreateVirtualCharacterDto
  ) {
    try {
      const response = await this.virtualCharacterService.create(userId, createDto);
      return getResponse(SUCCESS_CODE.SUCCESS, SUCCESS_MESSAGE.SUCCESS, response);
    } catch (error) {
      return getResponse(ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL], null);
    }
  }

  // @Get(':id')
  // async findOne(
  //   @CurrentUser() userId: string,
  //   @Param('id') characterId: string
  // ) {
  //   try {
  //     const response = await this.virtualCharacterService.findOne(userId, characterId);
  //     if (!response) {
  //       return getResponse(ERROR_CODE.NOT_FOUND, 'Character not found', null);
  //     }
  //     return getResponse(SUCCESS_CODE.SUCCESS, SUCCESS_MESSAGE.SUCCESS, response);
  //   } catch (error) {
  //     return getResponse(ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL], null);
  //   }
  // }

  @Post('update')
  async update(
    @CurrentUser() userId: string,
    @Query('characterId') characterId: string,
    @Body() updateDto: UpdateVirtualCharacterDto,
  ) {
    try {
      if (!characterId) {
        return getResponse(ERROR_CODE.INVALID_INPUT, 'Character ID is required', null);
      }
      const updatedCharacter = await this.virtualCharacterService.update(userId, characterId, updateDto);
      if (!updatedCharacter) {
        return getResponse(ERROR_CODE.NOT_FOUND, 'Character not found', null);
      }
      return getResponse(SUCCESS_CODE.SUCCESS, SUCCESS_MESSAGE.SUCCESS, updatedCharacter);
    } catch (error) {
      return getResponse(ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL], null);
    }
  }

  // 查询所有称呼
  @Post('appellation/list')
  async getAllAppellations() {
    try {
      const response = await this.virtualCharacterService.findAllAppellations();
      return getResponse(SUCCESS_CODE.SUCCESS, SUCCESS_MESSAGE.SUCCESS, response);
    } catch (error) {
      return getResponse(ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL], null);
    }
  }

  // 查询所有关系
  @Post('relationship/list')
  async getAllRelationships() {
    try {
      const response = await this.virtualCharacterService.findAllRelationships();
      return getResponse(SUCCESS_CODE.SUCCESS, SUCCESS_MESSAGE.SUCCESS, response);
    } catch (error) {
      return getResponse(ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL], null);
    }
  }

  // 查询所有音色
  @Post('voice-tone/list')
  async getAllVoiceTones() {
    try {
      const response = await this.virtualCharacterService.findAllVoiceTones();
      return getResponse(SUCCESS_CODE.SUCCESS, SUCCESS_MESSAGE.SUCCESS, response);
    } catch (error) {
      return getResponse(ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL], null);
    }
  }

  // 查询所有预设人设
  @Post('character-setting/list')
  async getAllCharacterSettings() {
    try {
      const response = await this.virtualCharacterService.findAllCharacterSettings();
      return getResponse(SUCCESS_CODE.SUCCESS, SUCCESS_MESSAGE.SUCCESS, response);
    } catch (error) {
      return getResponse(ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL], null);
    }
  }

  // 按分类查询音色
  @Post('voice-tone/category')
  async getVoiceTonesByCategory(@Body('category') category: string) {
    try {
      if (!category) {
        return getResponse(ERROR_CODE.INVALID_INPUT, 'Category parameter is required', null);
      }
      const response = await this.virtualCharacterService.findVoiceTonesByCategory(category);
      return getResponse(SUCCESS_CODE.SUCCESS, SUCCESS_MESSAGE.SUCCESS, response);
    } catch (error) {
      return getResponse(ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL], null);
    }
  }

  // 查询VIP内容
  @Post('vip/list')
  async getVipContent(@Body('type') type: string) {
    try {
      let response;
      switch (type) {
        case 'appellation':
          response = await this.virtualCharacterService.findVipAppellations();
          break;
        case 'relationship':
          response = await this.virtualCharacterService.findVipRelationships();
          break;
        case 'voice-tone':
          response = await this.virtualCharacterService.findVipVoiceTones();
          break;
        case 'character-setting':
          response = await this.virtualCharacterService.findVipCharacterSettings();
          break;
        default:
          return getResponse(ERROR_CODE.INVALID_INPUT, 'Invalid type parameter', null);
      }
      return getResponse(SUCCESS_CODE.SUCCESS, SUCCESS_MESSAGE.SUCCESS, response);
    } catch (error) {
      return getResponse(ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL], null);
    }
  }

  @Get('find')
  async findLatest(
    @CurrentUser() userId: string
  ) {
    try {
      const response = await this.virtualCharacterService.findLatest(userId);
      if (!response) {
        return getResponse(ERROR_CODE.NOT_FOUND, 'No character found', null);
      }
      return getResponse(SUCCESS_CODE.SUCCESS, SUCCESS_MESSAGE.SUCCESS, response);
    } catch (error) {
      return getResponse(ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL], null);
    }
  }
} 